package com.cg.controller;

import com.cg.client.SystemClient;
import com.cg.dto.Result;
import com.cg.dto.RoleDto;
import com.cg.dto.RoleSourceDto;
import com.cg.entity.Role;
import com.cg.utils.TokenUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName RoleController
 * @Description TODO
 * @Author CG
 * @Date 2023/12/10 10:42
 **/
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private SystemClient systemClient;

    /**
     * 分页角色管理
     * @param roleDto
     * @return
     */
    @PostMapping("/list")
    public Result<Map<String,Object>> queryRoleByRoleName(RoleDto roleDto){
        return systemClient.queryRoleByRoleName(roleDto);
    }


    /**
     * 角色授权
     * @param roleSourceDto
     * @return
     */
    @PostMapping("/addSourceForRole")
    public Result createRoleSource(@RequestBody RoleSourceDto roleSourceDto){
        return systemClient.createRoleSource(roleSourceDto);
    }


    /**
     * 验证角色编码
     * @param roleCode
     * @return
     */
    @GetMapping("/checkRoleCode")
    public Result checkRoleCode(String roleCode){
        return systemClient.checkRoleCode(roleCode);
    }


    /**
     * 创建角色
     */
    @PostMapping("/insertRole")
    public Result insertRole(HttpServletRequest request,Role role){
        String token = request.getHeader("token");
        Integer userId = TokenUtil.getUserIdByToken(token);
        role.setCreateUser(userId);
        return systemClient.insertRole(role);
    }


    /**
     * 角色信息修改
     * @param role
     * @return
     */
    @PostMapping("/setRole")
    public Result updateRole(Role role){
        return systemClient.updateRole(role);
    }


    /**
     * 角色信息删除
     * @param roleId
     * @return
     */
    @PostMapping("/removeRole")
    public Result deleteRoleByRoleId(Integer roleId){
        return systemClient.deleteRoleByRoleId(roleId);
    }
}
